package com.meichis.yndms.vm;

import android.app.Application;
import android.content.Context;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;

import com.amap.api.location.AMapLocation;
import com.amap.api.maps2d.AMap;
import com.amap.api.maps2d.CameraUpdateFactory;
import com.amap.api.maps2d.model.LatLng;
import com.amap.api.maps2d.model.LatLngBounds;
import com.meichis.yndms.common.MapsImpl;
import com.meichis.yndms.model.ShopInfo;

/**
 * @author xmy
 * @title
 * @remake
 * @create 2021-03-13 0:30
 */
public class ClientLocationVM extends MyAndroidViewModel implements MapsImpl.LonLatListener {

    private AMap aMap;
    private Context context;
    private boolean isFirst = true;
    private MutableLiveData<ShopInfo> shopInfo;

    public MutableLiveData<ShopInfo> getShopInfo() {
        if (shopInfo == null){
            shopInfo = new MutableLiveData<>();
        }
        return shopInfo;
    }

    public ClientLocationVM(@NonNull Application application) {
        super(application);
        this.context = application.getApplicationContext();
    }


    public void initMap(AMap aMap) {
        getShowDialog().setValue(true);
        this.aMap = aMap;
        MapsImpl.getInstance().initMap(aMap, context, this);
    }


    @Override
    public void onLocationChanged(AMapLocation aMapLocation) {
        if (isFirst) {
            getShowDialog().setValue(false);
            if (aMap != null && aMapLocation != null) {
                aMap.moveCamera(CameraUpdateFactory.changeLatLng(new LatLng(aMapLocation.getLatitude(), aMapLocation.getLongitude())));
                LatLngBounds.Builder builder = new LatLngBounds.Builder();
                LatLngBounds bounds = null;
                builder.include(new LatLng(aMapLocation.getLatitude(), aMapLocation.getLongitude()));//把你所有的坐标点放进去
              //  builder.include(new LatLng(inspectTaskSchedule.getLatitude(), inspectTaskSchedule.getLongitude()));//把你所有的坐标点放进去
                bounds = builder.build();
                aMap.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds, 240));
            }
            isFirst = false;
        }
    }
}
